package android.app;

import android.media.AudioAttributes;
import android.net.StringUri;
import android.net.Uri;

/**
 * @author slc
 * @date 2022/03/30 下午 4:28
 */
public class NotificationChannel {
    
    public static final String DEFAULT_CHANNEL_ID = "miscellaneous";

    public static final String CONVERSATION_CHANNEL_ID_FORMAT = "%1$s : %2$s";

    public static final String PLACEHOLDER_CONVERSATION_ID = ":placeholder_id";

    private static final int MAX_TEXT_LENGTH = 1000;

    private static final String TAG_CHANNEL = "channel";
    private static final String ATT_NAME = "name";
    private static final String ATT_DESC = "desc";
    private static final String ATT_ID = "id";
    private static final String ATT_DELETED = "deleted";
    private static final String ATT_PRIORITY = "priority";
    private static final String ATT_VISIBILITY = "visibility";
    private static final String ATT_IMPORTANCE = "importance";
    private static final String ATT_LIGHTS = "lights";
    private static final String ATT_LIGHT_COLOR = "light_color";
    private static final String ATT_VIBRATION = "vibration";
    private static final String ATT_VIBRATION_ENABLED = "vibration_enabled";
    private static final String ATT_SOUND = "sound";
    private static final String ATT_USAGE = "usage";
    private static final String ATT_FLAGS = "flags";
    private static final String ATT_CONTENT_TYPE = "content_type";
    private static final String ATT_SHOW_BADGE = "show_badge";
    private static final String ATT_USER_LOCKED = "locked";
    private static final String ATT_FG_SERVICE_SHOWN = "fgservice";
    private static final String ATT_GROUP = "group";
    private static final String ATT_BLOCKABLE_SYSTEM = "blockable_system";
    private static final String ATT_ALLOW_BUBBLE = "allow_bubbles";
    private static final String ATT_ORIG_IMP = "orig_imp";
    private static final String ATT_PARENT_CHANNEL = "parent";
    private static final String ATT_CONVERSATION_ID = "conv_id";
    private static final String ATT_IMP_CONVERSATION = "imp_conv";
    private static final String ATT_DEMOTE = "dem";
    private static final String DELIMITER = ",";

    /**
     * @hide
     */
    public static final int USER_LOCKED_PRIORITY = 0x00000001;
    /**
     * @hide
     */
    public static final int USER_LOCKED_VISIBILITY = 0x00000002;
    /**
     * @hide
     */
    public static final int USER_LOCKED_IMPORTANCE = 0x00000004;
    /**
     * @hide
     */
    public static final int USER_LOCKED_LIGHTS = 0x00000008;
    /**
     * @hide
     */
    public static final int USER_LOCKED_VIBRATION = 0x00000010;
    /**
     * @hide
     */
    public static final int USER_LOCKED_SOUND = 0x00000020;

    /**
     * @hide
     */
    public static final int USER_LOCKED_SHOW_BADGE = 0x00000080;

    /**
     * @hide
     */
    public static final int USER_LOCKED_ALLOW_BUBBLE = 0x00000100;

    /**
     * @hide
     */
    public static final int[] LOCKABLE_FIELDS = new int[]{
            USER_LOCKED_PRIORITY,
            USER_LOCKED_VISIBILITY,
            USER_LOCKED_IMPORTANCE,
            USER_LOCKED_LIGHTS,
            USER_LOCKED_VIBRATION,
            USER_LOCKED_SOUND,
            USER_LOCKED_SHOW_BADGE,
            USER_LOCKED_ALLOW_BUBBLE
    };

    /**
     * @hide
     */
    public static final int DEFAULT_ALLOW_BUBBLE = -1;
    /**
     * @hide
     */
    public static final int ALLOW_BUBBLE_ON = 1;
    /**
     * @hide
     */
    public static final int ALLOW_BUBBLE_OFF = 0;

    private static final int DEFAULT_LIGHT_COLOR = 0;
    private static final int DEFAULT_VISIBILITY =
            NotificationManager.VISIBILITY_NO_OVERRIDE;
    private static final int DEFAULT_IMPORTANCE =
            NotificationManager.IMPORTANCE_UNSPECIFIED;
    private static final boolean DEFAULT_DELETED = false;
    private static final boolean DEFAULT_SHOW_BADGE = true;

    private String mId;
    private String mName;
    private String mDesc;
    private int mImportance = DEFAULT_IMPORTANCE;
    private int mOriginalImportance = DEFAULT_IMPORTANCE;
    private boolean mBypassDnd;
    private int mLockscreenVisibility = DEFAULT_VISIBILITY;
    private Uri mSound = new StringUri();
    private boolean mLights;
    private int mLightColor = DEFAULT_LIGHT_COLOR;
    private long[] mVibration;
    private int mUserLockedFields;
    private boolean mFgServiceShown;
    private boolean mVibrationEnabled;
    private boolean mShowBadge = DEFAULT_SHOW_BADGE;
    private boolean mDeleted = DEFAULT_DELETED;
    private String mGroup;
    private AudioAttributes mAudioAttributes;
    private boolean mBlockableSystem = false;
    private int mAllowBubbles = DEFAULT_ALLOW_BUBBLE;
    private boolean mImportanceLockedByOEM;
    private boolean mImportanceLockedDefaultApp;
    private String mParentId = null;
    private String mConversationId = null;
    private boolean mDemoted = false;
    private boolean mImportantConvo = false;

    public NotificationChannel(String id, CharSequence name, int importance) {
        this.mId = getTrimmedString(id);
        this.mName = name != null ? getTrimmedString(name.toString()) : null;
        this.mImportance = importance;
    }

    public String getId() {
        return mId;
    }

    public void setId(String mId) {
        this.mId = mId;
    }

    public String getName() {
        return mName;
    }

    public void setName(String mName) {
        this.mName = mName;
    }

    public String getDesc() {
        return mDesc;
    }

    public void setDesc(String mDesc) {
        this.mDesc = mDesc;
    }

    public int getImportance() {
        return mImportance;
    }

    public void setImportance(int mImportance) {
        this.mImportance = mImportance;
    }

    public int getOriginalImportance() {
        return mOriginalImportance;
    }

    public void setOriginalImportance(int mOriginalImportance) {
        this.mOriginalImportance = mOriginalImportance;
    }

    public boolean ismBypassDnd() {
        return mBypassDnd;
    }

    public void setBypassDnd(boolean mBypassDnd) {
        this.mBypassDnd = mBypassDnd;
    }

    public int getLockscreenVisibility() {
        return mLockscreenVisibility;
    }

    public void setLockscreenVisibility(int mLockscreenVisibility) {
        this.mLockscreenVisibility = mLockscreenVisibility;
    }

    public Uri getSound() {
        return mSound;
    }

    public void setSound(Uri mSound) {
        this.mSound = mSound;
    }

    public boolean ismLights() {
        return mLights;
    }

    public void setLights(boolean mLights) {
        this.mLights = mLights;
    }

    public int getLightColor() {
        return mLightColor;
    }

    public void setLightColor(int mLightColor) {
        this.mLightColor = mLightColor;
    }

    public long[] getVibration() {
        return mVibration;
    }

    public void setVibration(long[] mVibration) {
        this.mVibration = mVibration;
    }

    public int getUserLockedFields() {
        return mUserLockedFields;
    }

    public void setUserLockedFields(int mUserLockedFields) {
        this.mUserLockedFields = mUserLockedFields;
    }

    public boolean ismFgServiceShown() {
        return mFgServiceShown;
    }

    public void setFgServiceShown(boolean mFgServiceShown) {
        this.mFgServiceShown = mFgServiceShown;
    }

    public boolean ismVibrationEnabled() {
        return mVibrationEnabled;
    }

    public void setVibrationEnabled(boolean mVibrationEnabled) {
        this.mVibrationEnabled = mVibrationEnabled;
    }

    public boolean ismShowBadge() {
        return mShowBadge;
    }

    public void setShowBadge(boolean mShowBadge) {
        this.mShowBadge = mShowBadge;
    }

    public boolean ismDeleted() {
        return mDeleted;
    }

    public void setDeleted(boolean mDeleted) {
        this.mDeleted = mDeleted;
    }

    public String getGroup() {
        return mGroup;
    }

    public void setGroup(String mGroup) {
        this.mGroup = mGroup;
    }

    public AudioAttributes getAudioAttributes() {
        return mAudioAttributes;
    }

    public void setAudioAttributes(AudioAttributes mAudioAttributes) {
        this.mAudioAttributes = mAudioAttributes;
    }

    public boolean ismBlockableSystem() {
        return mBlockableSystem;
    }

    public void setBlockableSystem(boolean mBlockableSystem) {
        this.mBlockableSystem = mBlockableSystem;
    }

    public int getAllowBubbles() {
        return mAllowBubbles;
    }

    public void setAllowBubbles(int mAllowBubbles) {
        this.mAllowBubbles = mAllowBubbles;
    }

    public boolean ismImportanceLockedByOEM() {
        return mImportanceLockedByOEM;
    }

    public void setImportanceLockedByOEM(boolean mImportanceLockedByOEM) {
        this.mImportanceLockedByOEM = mImportanceLockedByOEM;
    }

    public boolean ismImportanceLockedDefaultApp() {
        return mImportanceLockedDefaultApp;
    }

    public void setImportanceLockedDefaultApp(boolean mImportanceLockedDefaultApp) {
        this.mImportanceLockedDefaultApp = mImportanceLockedDefaultApp;
    }

    public String getParentId() {
        return mParentId;
    }

    public void setParentId(String mParentId) {
        this.mParentId = mParentId;
    }

    public String getConversationId() {
        return mConversationId;
    }

    public void setConversationId(String mConversationId) {
        this.mConversationId = mConversationId;
    }

    public boolean ismDemoted() {
        return mDemoted;
    }

    public void setDemoted(boolean mDemoted) {
        this.mDemoted = mDemoted;
    }

    public boolean ismImportantConvo() {
        return mImportantConvo;
    }

    public void setImportantConvo(boolean mImportantConvo) {
        this.mImportantConvo = mImportantConvo;
    }

    public void enableLights(boolean lights) {
        this.mLights = lights;
    }

    public void enableVibration(boolean vibration) {
        this.mVibrationEnabled = vibration;
    }

    public void setSound(Uri sound, AudioAttributes audioAttributes) {
        this.mSound = sound;
        this.mAudioAttributes = audioAttributes;
    }

    public void setName(CharSequence name) {
        mName = name != null ? getTrimmedString(name.toString()) : null;
    }

    public void setDescription(String description) {
        mDesc = getTrimmedString(description);
    }

    private String getTrimmedString(String input) {
        if (input != null && input.length() > MAX_TEXT_LENGTH) {
            return input.substring(0, MAX_TEXT_LENGTH);
        }
        return input;
    }
}
